﻿using System;
using System.Collections.Generic;
using System.Linq;
using Interfaces.FilterModels;
using Interfaces.Filters;
using Interfaces.Models;

namespace BusinessLogic.Models.Filter.GenericFilters
{
    public class QuoteFilter : PriceFilter<IQuote, IQuoteisFilterModel>, IQuoteisFilter
    {
        public QuoteFilter(IQuoteisFilterModel filterModel) : base(filterModel)
        {
            FilterModel = filterModel;
        }

        public override IEnumerable<object> Filtrate(IEnumerable<object> source)
        {
            var result = base.Filtrate(source);
            if (result != null)
            {
                var target = result as IEnumerable<IQuote>;
                if (target == null)
                    throw new ArgumentException("Source has incorrect type.");
                return target.Where(GetVolumeFilter());
            }
            return null;
        }

        protected virtual Func<IQuote, bool> GetVolumeFilter()
        {
            var lowVolume = FilterModel.LowVolume ?? int.MinValue;
            var highVolume = FilterModel.HighVolume ?? int.MaxValue;
            return item => item.Volume <= highVolume && item.Volume >= lowVolume;
        }

        public IQuoteisFilterModel FilterModel { get; set; }
    }


}
